package com.cn.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cn.api.resource.BaseResultVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BaseController {
	private static final Log LOGGER = LogFactory.getLog(BaseController.class);

	SerializerFeature[] feature = { SerializerFeature.DisableCircularReferenceDetect,
			SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty,
			SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteMapNullValue };
	public static final String Res_Type = "application/json; charset=utf-8";
	public static final String Res_Type_Form = "application/x-www-form-urlencoded; charset=utf-8";

	/**
	 * @description: 构造成功返回结果
	 * @author: Wind-spg
	 * @param resultData
	 *            : 需要返回的数据，可选
	 * @return
	 */
	protected String buildSuccessResultInfo(Object resultData) {
		LOGGER.debug(String.format("enter function, %s", resultData));
		BaseResultVo resultVo = new BaseResultVo();
		resultVo.setResultData(resultData);
		resultVo.setResultMessage("success");
		return JSON.toJSONString(resultVo, feature);
	}
	protected String buildSuccessResultInfo(Object resultData,long resultTotal) {
		LOGGER.debug(String.format("enter function, %s", resultData));
		BaseResultVo resultVo = new BaseResultVo();
		resultVo.setResultMessage("success");
		resultVo.setResultData(resultData);
		resultVo.setResultTotal(resultTotal);
		return JSON.toJSONString(resultVo, feature);
	}
	protected JSONObject buildSuccessResult(Object resultData) {
		JSONObject json = new JSONObject();
		json.put("sign", resultData);
		return json;
	}

	/**
	 * @description: 构造失败返回结果
	 * @author: Wind-spg
	 * @param resultCode
	 *            :任意非0数字，代表失败
	 * @param failedMsg
	 *            ：失败信息
	 * @return
	 */
	protected String buildFailedResultInfo(int resultCode, String failedMsg) {
		BaseResultVo resultVo = new BaseResultVo(resultCode, failedMsg);
		return JSON.toJSONString(resultVo, feature);
	}

}
